/*
 * @(#) MainBody.js
 *
 * <br> Copyright:  Copyright (c) 2017
 * <br>
 * <br> @author caijinhuang
 * <br> 2017-11-19 22:14:34
 */

import React from 'react';
import '@share/shareui-font/dist/style.css';
import '@share/shareui-html';
import {
    Icon,
    Button,
    Table,
    Breadcrumb,
    Panel
} from '@share/shareui';
import '../../../css/main.css'
import MeetingEdit from "../createmeeting/Meeting";
import {ModalRegist,ModalManage,ModalPassManage} from "./ModalShow";
import ExcelInput from "./ExcelInput";
import {UserUtil} from "../../UserUtil"

class MainBody extends React.Component {

    static title = "";// 要检索的会议标题

    constructor(props){
        super(props);
        this.state = {
            showModal:false,
            showManage:false,
            showRegist:false,
            showPassManage:false,

            tables:[],
            isCreate:false,
            showDetail:false,
            id:'',//新建会议的id
            selectId:'',//要显示的会议详情的id
        };
    }

    componentDidUpdate(){
        console.info("界面更新完成",UserUtil.userInfo);
        if(MainBody.title === ""){
            this.renderTable();
        }
        MainBody.title="";
    }
    /**
     *  搜索结果渲染
     */
    resetList(title){
        this.state.showDetail = false;
        MainBody.title = title;
        this.renderTable();
        this.setState({isCreate:false});
    }

    /*渲染列表*/
    renderTable(){
        let opts = {
                basePath: "node_modules/@share/ulynlist-ext",
                tableTpl:"share",
                tableColumn:{
                    title:'ulynlist',
                    rememberCheckbox:true,
                    keyColumn:"",
                    columns:[
                        {field:'MEETING_TITLE',overflowview:'ellipsis',label:'会议名称',className:"unSortable"},
                        {field:'MEETING_TIME',overflowview:'ellipsis',label:'记录时间',className:"sortable"},
                        {field:'MEETING_ID', label: '操作', style: "text-align:left", className: "sortable",width:"120",
                            bodyContent:'<a class="table_do_a btn_view toDetail" href="#/detail?id=[MEETING_ID]" name="detail">详情</a>'
                        }
                    ],
                    rownumbers:false,
                },
                extra:{
                    linesPerPageEditable:true,
                    tableSize:'',/*默认值为default，配置值支持 sm*/
                    lineStyle:'lineSpace'/*默认值为default，配置值支持 lineSpace(各行变色)*/
                },
                usePageBar:true,
                requestData:{listSql:"getMeetings",linesPerPage:5,currentPage:1},
                customAjax:function(requestData,callback){
                    let baseUrl = '/sharemeeting/ulynlist?';
                    if(MainBody.title !== ""){
                        requestData.listSql = "getMeetingByTitle"
                    }
                    $.ajax({
                               url: baseUrl
                                    + "listSql=" + requestData.listSql
                                    + "&linesPerPage=" + requestData.linesPerPage
                                    + "&currentPage=" + requestData.currentPage
                                    + "&userid=" + UserUtil.userInfo.userId
                                    + "&title=" + MainBody.title,
                               type:"get",
                               contentType:"application/json",
                               dataType:"json",
                               success:function(result){
                                   if(result.state === false){
                                       alert("获取失败");
                                   }else{

                                       let data = {
                                           status  : true,
                                           data  : {
                                               list: result.data.list,
                                               currentPage: result.data.currentPage,
                                               totalNum: result.data.totalNum,
                                               linesPerPage: requestData.linesPerPage
                                           }
                                       }
                                       console.log(requestData.linesPerPage);
                                       callback(data);
                                   }
                               },
                               error:function(data){
                                   // alert("网络错误，请重试");
                               }
                           });
                },
                pageBarId: 'ulyn-pageBar-id',
                pageBarTpl:"share"
            };
        $("#ulyn-table-id").ulynlist(opts);
        $("#imeetings").trigger("refresh");
    }


    /*弹窗*/
    close(){this.setState({showModal: false,showManage:false,showRegist:false,showPassManage:false})};
    show(){this.setState({showModal: true})};
    showManage(){this.setState({showManage:true})};
    showRegist(){this.setState({showRegist:true});this.state.showRegist=false};
    showPassManage(){this.setState({showPassManage:true})}

    /*新建会议*/
    createMeeting(){
        let url = "/sharemeeting/meeting/savaMeeting.do";
        /*异步请求*/
        fetch(url,{
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            credentials: 'include',
            body:JSON.stringify({}),
        }).then((response) => response.json()) //把response转为json
            .then((responseData) => { // 上面的转好的json
                if(responseData.status === true){
                    console.log(responseData.data.id);
                    this.comeToMeeting(responseData.data.id);
                }else{
                    alert(responseData.msg);
                    window.location.href="#/";
                }
            }).catch((error)=> {})
    }


    comeToMeeting(id){
        this.state.id = id;
        this.setState({isCreate:true});
    }

    retBack(){
        if(window.confirm("确定要放弃编辑吗？")){
            window.location.reload();
        }
    }

    render() {
        if(this.state.isCreate){
            return(
                <div className="editBody">
                    <div className="formGroup">
                        <div className="formTitle">
                            <Breadcrumb icon>
                                <Breadcrumb.Item onClick={this.retBack.bind(this)}>
                                    首页
                                </Breadcrumb.Item>
                                <Breadcrumb.Item active>
                                    新建会议
                                </Breadcrumb.Item>
                            </Breadcrumb>
                        </div>
                        <MeetingEdit userInfo={this.props.userInfo} id={this.state.id}/>
                    </div>
                </div>
            );
        }else{
            return (
                <div>
                    <div className="nav_left">
                        <ul className="nav_ul">
                            {/*<li><Button bsClass="btn btn_primary" onClick={this.createMeeting.bind(this)}>新建</Button></li>*/}
                            {UserUtil.userInfo.roleName === "super_admin" ?
                             <li><Button disabled bsClass="btn btn_primary" onClick={this.createMeeting.bind(this)}>新建</Button></li>:
                              <li><Button bsSize="lg" bsClass="btn btn_primary" onClick={this.createMeeting.bind(this)}>新建</Button></li>}
                            <li className="tab" id="imeetings"><Icon className="fa fa-home"/> 我的会议</li>
                            {/*<li className="tab" onClick={this.initTable.bind(this,0)}><Icon className="fa fa-trash-o"/> 垃圾箱</li>*/}
                            {/*{this.props.userInfo.roleName === "super_admin" ?<li className="tab" onClick={this.initTable.bind(this,0)}><Icon className="fa fa-trash-o"/> 垃圾箱</li>:null}*/}
                            {UserUtil.userInfo.roleName === "super_admin" ?<li className="tab" onClick={this.showPassManage.bind(this)}><Icon className="fa fa-users"/> 用户管理</li>:null}
                            {UserUtil.userInfo.roleName === "super_admin" ?<li className="tab" onClick={this.showRegist.bind(this)}><Icon className="fa fa-user-plus"/> 用户注册</li>:null}
                            {UserUtil.userInfo.roleName === "super_admin" ?<li className="tab"><ExcelInput/></li>:null}
                        </ul>
                    </div>
                    <div className="nav_right">
                        <Panel>
                            <Panel.Body full={true}>
                                <div id="ulyn-table-id"/>
                                <div id="ulyn-pageBar-id"/>
                            </Panel.Body>
                        </Panel>
                    </div>
                    <ModalRegist showRegist={this.state.showRegist} close={this.close.bind(this)}/>
                    <ModalManage showManage={this.state.showManage} close={this.close.bind(this)}/>
                    <ModalPassManage showManage={this.state.showPassManage} close={this.close.bind(this)} userInfo={this.props.userInfo}/>
                </div>
            );
        }
    }
}
export default MainBody
